package org.porridge.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

/**
 * 操作文件工具类
 * 
 * @author porridge
 * 
 */
public class FileUtil {

	public static boolean writeText(String url, String content) {
		FileOutputStream fos;
		boolean ret = false;
		try {
			File file = new File(url);
			if (!file.exists())
				file.createNewFile();
			
			fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos,
					Constant.CHARSET);
			BufferedWriter bw = new BufferedWriter(osw);
			bw.write(content);
			bw.close();
			osw.close();
			fos.close();
			ret = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return ret;
		}
	}

	public static String readText(String url) {
		File file = new File(url);
		if(!file.exists())
			return null;
		
		StringBuffer stb = new StringBuffer();
		BufferedReader buff = null;
		try {
			buff = new BufferedReader(new FileReader(file));
			while (buff.ready()) {
				stb.append(buff.readLine());
			}
			buff.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			return stb.toString();
		}

	}
}
